package com.example.towSpringBoot.service.impl;

import com.example.towSpringBoot.common.PageQuery;
import com.example.towSpringBoot.common.PageResult;
import com.example.towSpringBoot.dto.*;
import com.example.towSpringBoot.entity.Department;
import com.example.towSpringBoot.entity.Employee;
import com.example.towSpringBoot.mapper.DepartmentMapper;
import com.example.towSpringBoot.mapper.EmployeeMapper;
import com.example.towSpringBoot.mapper.EmployeeRoleMapper;
import com.example.towSpringBoot.service.EmployeeService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xinTong
 * @since 2024-01-23
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;

    /**
     * 员工查询
     * @param queryDto
     * @return
     */
    @Override
    public PageResult list(EmployeePageQueryDto queryDto) {

        int pageNum = queryDto.getPageNum();
        int pageSize = queryDto.getPageSize();

        // 先设置PageHelper必要参数
        PageHelper.startPage(pageNum, pageSize);
        // TODO: 差一个高级查询

        Page<EmployeeDto> page = employeeMapper.list(queryDto);

        // 将Page数据赋值到PageResult中，展示给前端
        PageResult pageResult = new PageResult();
        pageResult.setList(page);
        pageResult.setTotal(page.getTotal());
        pageResult.setPageNum(page.getPageNum());
        pageResult.setPageSize(page.getPageSize());

        return pageResult;
    }

    /**
     * 删除员工
     * @param id
     */
    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    /**
     * 查询员工详细信息
     *
     * @param id
     * @return
     */
    @Override
    public EmployeeDto info(Long id) {
        EmployeeDto employeeDto = employeeMapper.info(id);
        return employeeDto;
    }

    /**
     * 更新或者新增员工信息
     *
     * @param employeeRoleDto
     */
    @Override
    public void saveOrUpdate(EmployeeRoleDto employeeRoleDto) {
        Employee employee = employeeRoleDto.getEmployee();

        employee.setDept_id(employee.getDepartment().getId());

        Long id = employee.getId();
        List<Integer> roleIds = employeeRoleDto.getRoleIds();
        // 更新
        if (employee.getId() != null) {
            employeeMapper.update(employee);
            //TODO:修改权限，有增有减
            // 如果不论性能，直接全部删除根据employee.getId(),再全部重新添加权限
            employeeRoleMapper.deleteRole(id);

            for (Integer roleId : roleIds) {
                employeeRoleMapper.addRole(id, roleId);

            }
        }
        // 新增
        else {
            employeeMapper.save(employee);
            // 使用mybatis中《insert标签对中useGeneratedKeys="true" keyProperty="id" 再使用.getId()来获得插入数据的id
            Long saveId = employee.getId();

            // 循环多次插入员工权限数据
            for (Integer roleId : roleIds) {
                employeeRoleMapper.addRole(saveId, roleId);
            }
        }


    }

    @Override
    public void updateState(AdminDto adminDto) {
        Long id = adminDto.getId();
        Boolean just = adminDto.getAdmin();
        employeeMapper.admin(id,just);
    }

}
